#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>

using namespace std;
using LL = long long;
const int N = 5e4 + 10;

int n;
int a0, a1, b0, b1;
int prime[N], cnt;
bool st[N];
int divider[N], cntd;

struct factor{
    int s, p;
}fac[1610];
int fcnt;

//最小公倍数=两数的乘积/最大公约（因）数, 解题时要避免和最大公约（因）数问题混淆。

void init(){
    for(int i = 2; i < N; i ++){
        if(!st[i]) prime[cnt ++] = i;
        for(int j = 0; prime[j] < N / i; j ++){
            st[prime[j] * i] = true;
            if(i % prime[j] == 0) break;
        }
    }
}


int gcd(int x, int y){
    return y ? gcd(y, x % y) : x;
}

void dfs(int index, int num){
    if(index == fcnt){
        divider[cntd ++] = num;
        return;
    }

    for(int i = 0; i <= fac[index].p; i ++){ 
        dfs(index + 1, num);
        num *= fac[index].s;
    }
}


int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);

    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    init();

    cin >> n;
    while(n --){
        fcnt = 0;
        cin >> a0 >> a1 >> b0 >> b1;
        int d = b1;
        for(int i = 0; prime[i] <= b1 / prime[i]; i ++){
            if(b1 % prime[i] == 0){
                int s = 0;
                while(b1 % prime[i] == 0){
                    s ++;
                    b1 /= prime[i];
                }
                fac[fcnt ++] = {prime[i], s};
            }
        }

        if(b1 > 1) fac[fcnt++] = {b1, 1};

        // for(int i = 0; i < fcnt; i ++){
        //     cout << fac[i].s << " "  << fac[i].p << " |";
        // }
        // cout << endl;

        cntd = 0;
        dfs(0, 1);

        // for(int i = 0; i < cntd; i ++) cout << divider[i] << " ";
        // cout << endl;

        int res = 0;
        for(int i = 0; i < cntd; i ++){
            if(gcd(divider[i], a0) == a1 && (LL)b0 * divider[i] / gcd(divider[i], b0) == d){
                res ++;
            }
        }

        cout << res << endl;

    }
    

    
    return 0;
}